<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
      <el-form-item label="操作人" prop="operatorName">
        <el-input
          v-model="queryParams.operatorName"
          clearable
          placeholder="请输入操作人"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理结果" prop="status">
        <el-select v-model="queryParams.status" clearable filterable placeholder="请选择处理结果"
                   @keyup.enter.native="handleQuery">
          <el-option v-for="dict in dict.type.export_status" :key="dict.value" :label="dict.label"
                     :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="操作来源" prop="operatingSource">
        <el-select v-model="queryParams.operatingSource" clearable filterable placeholder="请选择操作来源"
                   @keyup.enter.native="handleQuery">
          <el-option v-for="dict in dict.type.export_operating_source" :key="dict.value" :label="dict.label"
                     :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="登记时间" prop="gmtCreate">
        <el-date-picker v-model="queryParams.gmtCreate"
                        clearable
                        placeholder="请选择登记时间"
                        type="date"
                        value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="logList" @selection-change="handleSelectionChange">
      <el-table-column align="center" label="批次号" prop="batchNumber"/>
      <el-table-column align="center" label="文件来源" prop="exportSource">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.export_file_source" :value="scope.row.exportSource"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="导出状态" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.export_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作时间" prop="gmtCreate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtCreate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作人" prop="operatorName"/>
      <el-table-column align="center" label="操作来源" prop="operatingSource">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.export_operating_source" :value="scope.row.operatingSource"/>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button v-hasPermi="['resource:exportLog:downLoadLocal']" icon="el-icon-edit" size="mini" type="text"
                     @click="downLoadLocal(scope.row)">下载
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total"
                @pagination="getList"/>

  </div>
</template>

<script>
import {listLog} from "@/api/resource/download";

export default {
  name: "Download",
  dicts: ['export_operating_source', 'export_status', 'export_file_source'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 导出记录表格数据
      logList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        batchNumber: null,
        operatorId: null,
        operatorName: null,
        exportSource: null,
        fileName: null,
        total: null,
        batchSize: null,
        remainBatch: null,
        exportType: null,
        exportDetails: null,
        status: null,
        operatingSource: null,
        gmtCreate: null,
        gmtModified: null,
        orderByColumn: 'gmtCreate',
        isAsc: "desc",
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询导出记录列表 */
    getList() {
      this.loading = true;
      listLog(this.queryParams).then(response => {
        this.logList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        batchNumber: null,
        operatorId: null,
        operatorName: null,
        exportSource: null,
        fileName: null,
        total: null,
        batchSize: null,
        remainBatch: null,
        exportType: null,
        exportDetails: null,
        status: 0,
        operatingSource: null,
        gmtCreate: null,
        gmtModified: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加导出记录";
    },
    /** 导出按钮操作 */
    downLoadLocal(row) {
      if (row.status != 2) {
        this.$modal.msgWarning("当前任务非处理成功，请稍后下载");
        return;
      }
      var url = row.exportDetails;
      if (url == "" || url == null) {
        this.$modal.msgWarning("该记录已过期，无法下载");
        return;
      }
      //0 本地 1 OSS
      if (row.exportType == 1) {
        window.location.href = url;
      } else {
        this.download('resource/exportLog/downLoadLocal', {
          ...row
        }, `下载数据_${new Date().getTime()}.xlsx`)
      }
    }
  }
};
</script>
